<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>31-带参数的过滤器</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style></style>
  </head>
  <body>
    <div id="app">
      <!--2： 调用格式化 -->
      <span>{{date | format()}}</span>
    </div>
    <script>
      // 3：封装格式化函数，将时间格式化
      function Zero(newDate) {
        return newDate <= 10 ? '0' + newDate : newDate;
      }
      Vue.filter('format', function (val) {
        function dateFormat(date) {
          console.log(date);
          var y = val.getFullYear();
          Zero(y);
          var M = val.getMonth() + 1;
          Zero(M);
          var d = val.getDate();
          Zero(d);
          var h = val.getHours();
          Zero(h);
          var m = val.getMinutes();
          Zero(m);
          var s = val.getSeconds();
          Zero(s);
          return (
            y +
            '年' +
            M +
            '月' +
            d +
            '日' +
            ' - ' +
            h +
            '时' +
            m +
            '分' +
            s +
            '秒'
          );
        }
        return dateFormat(val);
      });
      console.log();
      var vm = new Vue({
        el: '#app',
        data: {
          date: new Date(), // 1: 定义时间
        },
        methods: {},
      });
    </script>
  </body>
</html>
